package com.link2room.rodimus.service.impl;

import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.exception.SysException;
import com.link2room.common.util.DateUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.dto.result.base.DrawPrizeBaseDto;
import com.link2room.rodimus.entity.DrawPrizeBaseEntity;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.feign.soundwave.PmsMemberFeignService;
import com.link2room.rodimus.service.IAfterDrawHandleService;
import com.link2room.soundwave.dto.param.inside.UsableCouopnsInsideParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.DRAW_ERROR;
import static com.link2room.enums.exception.RodimusSysExceptionEnum.REDO_DRAW_AFTER_DO_ERROR;

/**
 * Created by LN on 2017/10/26
 */
@Component("pmsCouponAfterDrawHandleService")
public class AfterDrawHandlePMSCouponServiceImpl extends AbstractArfterDrawHandlerServiceImpl implements IAfterDrawHandleService {

    private static class  RuleMapKey{
        static final String couponType = "couponType";
        static final String startStep = "startStep";
        static final String dayNum = "dayNum";
    }
    /**
     * CouponType 类型   -   有效期设置
     */
    private static class CouponType {
        /**
         * 动态-
         */
        static final String Dynamic = "dynamic";
        /**
         * 静态-
         */
        static final String Static = "static";
    }

    @Autowired
    private PmsMemberFeignService pmsMemberFeignService;

    @Override
    void doAfterDraw(DrawPrizeBaseDto drawPrizeBaseDto) throws Exception {
        String rule = drawPrizeBaseDto.getRule();
        if (StringUtil.isBlank(rule)) {
            throw new SysException(DRAW_ERROR, "奖项未配置.[prizeCode ] " + drawPrizeBaseDto.getCode());
        }
        HashMap ruleMap = JacksonUtil.jsonToBean(rule, HashMap.class);
        String couponType = (String) ruleMap.get(RuleMapKey.couponType);
        UsableCouopnsInsideParamDto paramDto ;
        if (CouponType.Static.equals(couponType)) {
            paramDto = JacksonUtil.jsonToBean(rule, UsableCouopnsInsideParamDto.class);
        } else if(CouponType.Dynamic.equals(couponType)) {
            Long startStep = Long.valueOf((String) ruleMap.get(RuleMapKey.startStep));
            L2RAssert.numberNull(startStep,"[startStep] 未设置");
            Long dayNum =  Long.valueOf((String) ruleMap.get(RuleMapKey.dayNum));
            L2RAssert.numberNull(dayNum,"[dayNum] 未设置");
            Date startDatetime = DateUtil.addDays(new Date(), startStep);
            Date endDateTime = DateUtil.addDays(startDatetime, dayNum);
            paramDto = JacksonUtil.jsonToBean(rule, UsableCouopnsInsideParamDto.class);
            paramDto.setStartDatetime(DateUtil.getNewFormatDateString(startDatetime));
            paramDto.setEndDatetime(DateUtil.getNewFormatDateString(endDateTime));
        } else {
            throw new SysException(DRAW_ERROR,"CouponType 类型  未设置 .[prizeCode] "+drawPrizeBaseDto.getCode());
        }
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String memberNo = memberSession.getMemberNo();
        String lhotelGroupCode = memberSession.getLhotelGroupCode();
        paramDto.setL2RMemberNo(memberNo);
        paramDto.setLhotelGroupCode(lhotelGroupCode);
        pmsMemberFeignService.saleCoupons(paramDto);
    }



    @Override
    void redoAfterDraw(MemberBase memberBase, String openid, DrawPrizeBaseEntity prizeBase) throws Exception {
        L2RAssert.isTrue(memberBase == null,new SysException(REDO_DRAW_AFTER_DO_ERROR, prizeBase.getPrizeType()+" 需会员."+openid+" 不是会员"));
        String rule = prizeBase.getRule();
        if (StringUtil.isBlank(rule)) {
            throw new SysException(DRAW_ERROR, "奖项未配置.[prizeCode ] " + prizeBase.getCode());
        }
        HashMap ruleMap = JacksonUtil.jsonToBean(rule, HashMap.class);
        String couponType = (String) ruleMap.get(RuleMapKey.couponType);
        UsableCouopnsInsideParamDto paramDto ;
        if (CouponType.Static.equals(couponType)) {
            paramDto = JacksonUtil.jsonToBean(rule, UsableCouopnsInsideParamDto.class);
        } else if(CouponType.Dynamic.equals(couponType)) {
            Long startStep = Long.valueOf((String) ruleMap.get(RuleMapKey.startStep));
            L2RAssert.numberNull(startStep,"[startStep] 未设置");
            Long dayNum =  Long.valueOf((String) ruleMap.get(RuleMapKey.dayNum));
            L2RAssert.numberNull(dayNum,"[dayNum] 未设置");
            Date startDatetime = DateUtil.addDays(new Date(), startStep);
            Date endDateTime = DateUtil.addDays(startDatetime, dayNum);
            paramDto = JacksonUtil.jsonToBean(rule, UsableCouopnsInsideParamDto.class);
            paramDto.setStartDatetime(DateUtil.getNewFormatDateString(startDatetime));
            paramDto.setEndDatetime(DateUtil.getNewFormatDateString(endDateTime));
        } else {
            throw new SysException(DRAW_ERROR,"CouponType 类型  未设置 .[prizeCode] "+prizeBase.getCode());
        }

        String memberNo = memberBase.getMemberNo();
        String lhotelGroupCode = memberBase.getLhotelGroupCode();
        paramDto.setL2RMemberNo(memberNo);
        paramDto.setLhotelGroupCode(lhotelGroupCode);
        pmsMemberFeignService.saleCoupons(paramDto);
    }


}
